<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>平台组件管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript"
            src="../lib/vue/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/global.js"></script>
    <script type="text/javascript" src="../js/store.js"></script>
    <script type="text/javascript" src="../js/cookies.js"></script>
    <script src="../lib/vue/vue.min.js"></script>
    <script src="../lib/vue/axios.min.js"></script>
</head>
<body>
<div class="x-body layui-anim layui-anim-up">
    <form class="layui-form">
        <div class="layui-form-item">
            <div class="layui-inline" style="width: 300px;">
                <label for="L_companyKey" class="layui-form-label">公司</label>
                <div class="layui-input-block">
                    <select name="companyId" lay-verify="companyKey" lay-filter="companyKey" id="L_companyKey">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline" style="width: 300px;">
                <label for="L_userName" class="layui-form-label">系统</label>
                <div class="layui-input-block">
                    <select name="userId" lay-verify="userKey" lay-filter="userKey" id="L_userName">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline" style="width: 300px;">
                <label for="L_componentName" class="layui-form-label">组件</label>
                <div class="layui-input-block">
                    <select name="componentId" lay-verify="componentKey" lay-filter="componentKey"
                            id="L_componentName">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline" style="width: 300px;">
                <label for="L_versionNo" class="layui-form-label">组件版本</label>
                <div class="layui-input-block">
                    <input type="text" id="L_versionNo" name="versionNo" readonly="readonly"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline" style="width: 300px;">
                <label for="L_envName" class="layui-form-label">组件环境</label>
                <div class="layui-input-block">
                    <select name="envId" lay-verify="envNameKey" id="L_envName">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="padding-top: 100px">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="add" lay-submit="">
                点击添加
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
            $ = layui.jquery;
            let form = layui.form
                , layer = layui.layer
            let companyArray;
            let userArray;
            let componentArray;

            let envArray;
            // 获取公司下拉列表
            $.get(_hostUrl + '/company', function (res) {
                if (res.responseCode != 200) {
                    layer.msg(res.responseMsg, {icon: 2, time: 1500});
                    return
                }
                companyArray = res.data;
                if (!companyArray || companyArray.length == 0) {
                    layer.msg("公司不存在，请先创建公司信息!")
                } else {
                    for (let i = 0; i < companyArray.length; i++) {
                        $('#L_companyKey').append(new Option(companyArray[i].name, companyArray[i].id))
                    }
                    $('#L_companyKey').val(parseInt(getUrlKey('id')));
                    form.render();
                    form.on('select(companyKey)', function (data) {
                        //联动获取用户下拉列表
                        $.get(_hostUrl + '/users/' + data.value, function (res) {
                            if (res.responseCode != 200) {
                                layer.msg(res.responseMsg, {icon: 2, time: 1500});
                                return
                            }
                            userArray = res.data;
                            if (!userArray || userArray.length == 0) {
                                layer.msg("所选公司不存在用户信息，请重新选择则公司或者为该公司添加用户!")
                            } else {
                                $('#L_userName').html("");
                                for (let i = 0; i < userArray.length; i++) {
                                    $('#L_userName').append(new Option(userArray[i].name, userArray[i].id))
                                }
                                $('#L_userName').val(parseInt(getUrlKey('id')));
                                form.render();
                            }
                        })
                    });
                }
            });
            //获取组件下拉表
            $.get(_hostUrl + '/components', function (res) {
                if (res.responseCode != 200) {
                    layer.msg(res.responseMsg, {icon: 2, time: 1500});
                    return
                }
                componentArray = res.data;
                if (!componentArray || componentArray.length == 0) {
                    layer.msg("还未创建组件信息!")
                } else {
                    for (let i = 0; i < componentArray.length; i++) {
                        $('#L_componentName').append(new Option(componentArray[i].name, componentArray[i].id))
                    }
                    $('#L_componentName').val(parseInt(getUrlKey('id')));
                    form.render();
                    //渲染组件版本(只读)
                    form.on('select(componentKey)', function (data) {
                        $.get(_hostUrl + '/component/' + data.value, function (res) {
                            $('#L_versionNo').val(res.data.versionNo);
                            form.render();
                        })
                    })
                }
            })
            //获取组件环境列表
            $.get(_hostUrl + '/envs', function (res) {
                if (res.responseCode != 200) {
                    layer.msg(res.responseMsg, {icon: 2, time: 1500});
                    return
                }
                envArray = res.data;
                if (!envArray || envArray.length == 0) {
                    layer.msg("还未创建组件环境信息!")
                } else {
                    for (let i = 0; i < envArray.length; i++) {
                        $('#L_envName').append(new Option(envArray[i].name, envArray[i].id))
                    }
                    $('#L_envName').val(parseInt(getUrlKey('id')));
                    form.render();
                }
            });
            //自定义验证规则
            form.verify({
                companyKey: function (value) {
                    //没选中value为null，坑
                    if (null == value) {
                        return '请先选择公司！'
                    }
                },
                userKey: function (value) {
                    if (null == value) {
                        return '请选择用户名'
                    }
                },
                componentKey: function (value) {
                    if (null == value) {
                        return '请选择组件'
                    }
                },
                envNameKey: function (value) {
                    if (null == value) {
                        return '请选择环境'
                    }
                }
            });
            //监听提交
            form.on('submit(add)', function (data) {
                $.post(_hostUrl + "/info/addUsage", data.field, function (res) {
                    if (res.responseCode == 201) {
                        layer.msg('添加成功', {
                            icon: 1,
                            time: 2000
                        }, function () {
                            // 获得frame索引
                            let index = parent.layer.getFrameIndex(window.name)
                            //关闭当前frame
                            parent.layer.close(index)
                            let table = parent.window.layui.table;
                            table.reload('queryInfoList')
                        });
                    } else {
                        layer.msg(res.responseMsg, {icon: 2, time: 1500})
                    }
                }, 'json');
                return false
            });

        }
    )
</script>
</body>

</html>